module planegame{

    export class PlaneFactory {

        public planeArray: Array<Plane>;
        public enemyArray: Array<Plane>;

        public constructor() {
            this.planeArray = new Array<Plane>();
            this.enemyArray = new Array<Plane>();
        }

        public getPlane(resName:string, name:string): Plane {
            let plane:Plane = null;
            let planeArray = this.planeArray;
            let length = planeArray.length;

            for(let i:number = 0;i<length;i++) {
                let v:Plane = planeArray[i];
                if(!v.used) {
                    plane = v;
                    break;
                }
            }
            if(plane == null) {
                plane = this.createPlane(resName, name);
            }

            plane.used = true;
            return plane;
        }

        // 创建飞机
        private createPlane(resName:string, name:string): Plane {
            let plane = BulletFactory.createPlane(resName);
            plane.name = name;
            this.planeArray.push(plane);
            return plane;
        }


        public getEnemy(resName:string, name:string): Plane {
            let enemy:Plane = null;
            let enemyArray = this.enemyArray;
            let length = enemyArray.length;

            for(let i:number = 0;i<length;i++) {
                let v:Plane = enemyArray[i];
                if(!v.used) {
                    enemy = v;
                    break;
                }
            }
            if(enemy == null) {
                enemy = this.createEnemy(resName, name);
            }
            enemy.used = true;
            return enemy;
        }

        // 创建飞机
        private createEnemy(resName:string, name:string): Plane {
            let enemyPlane = BulletFactory.createPlane(resName);
            enemyPlane.name = name;
            this.enemyArray.push(enemyPlane);
            return enemyPlane;
        }

    }
}